package com.mfangsoft.zhuangjialong.app.seller.service.impl;

import java.net.URI;
import java.net.URISyntaxException;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ExecutionException;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.mfangsoft.zhuangjialong.app.seller.mapper.SellerMapper;
import com.mfangsoft.zhuangjialong.app.seller.model.Seller;
import com.mfangsoft.zhuangjialong.app.seller.service.ISellerService;
import com.mfangsoft.zhuangjialong.common.model.Page;
import com.mfangsoft.zhuangjialong.common.model.ResponseMessage;
import com.mfangsoft.zhuangjialong.common.utils.CacheUtil;
import com.mfangsoft.zhuangjialong.common.utils.HttpClientUtil;
import com.mfangsoft.zhuangjialong.common.utils.MD5Util;
import com.mfangsoft.zhuangjialong.common.utils.RedisUtils;
import com.mfangsoft.zhuangjialong.common.utils.SysConstant;
import com.mfangsoft.zhuangjialong.integration.biotopeInfo.model.BiotopeInfo;
import com.mfangsoft.zhuangjialong.integration.user.model.UserEntity;
import com.mfangsoft.zhuangjialong.integration.usermanage.mapper.SellerPointEntityMapper;
import com.mfangsoft.zhuangjialong.integration.usermanage.model.SellerPointEntity;
import com.mongodb.util.JSON;

@Service("appSellerService")
public class SellerServiceImpl implements ISellerService {

	@Autowired
	SellerMapper sellerMapper;
	@Autowired
	private SellerPointEntityMapper sellerPointEntityMapper;
	
	
	@Override
	public ResponseMessage<Map<String,Object>> sellerLogin(Seller seller) {
		ResponseMessage<Map<String,Object>> message=new ResponseMessage<Map<String,Object>>();
		String user_pwd = MD5Util.MD5(seller.getUser_pwd());
		Map sellerLoginResult = this.getSellerLoginResult(seller.getZzh(), seller.getBmh(), seller.getUser_name(),user_pwd );
		UserEntity user = sellerMapper.selectIdFromUserByZzhBmh(seller.getZzh(),seller.getBmh());
		if(sellerLoginResult.get("data") != null){
			if("-1".equals(((Map)sellerLoginResult.get("data")).get("success").toString())){
				message.setCode("1");
				message.setMessage("组织号或部门号不正确！");
				return message;
			}
			if(user == null){
				message.setCode("1");
				message.setMessage("分公司或专卖店不存在！");
				return message;
			}
			if("-3".equals(((Map)sellerLoginResult.get("data")).get("success").toString())){
				message.setCode("1");
				message.setMessage("密码不正确！");
				return message;
			}
			if("-9".equals(((Map)sellerLoginResult.get("data")).get("success").toString())){
				message.setCode("1");
				message.setMessage("账号不正确！");
				return message;
			}
			if( ! "1".equals(((Map)sellerLoginResult.get("data")).get("Status").toString())){
				message.setCode("1");
				message.setMessage("账号不可用！");
				return message;
			}
		}
		Seller sellerInfo = sellerMapper.selectOneBuUserName(seller.getUser_name());
		if(sellerInfo == null){
			seller.setCreate_time(new Date());
			seller.setUser_pwd(user_pwd);
			seller.setName(((Map)sellerLoginResult.get("data")).get("empName")==null?null:((Map)sellerLoginResult.get("data")).get("empName").toString());
			seller.setPhone(((Map)sellerLoginResult.get("data")).get("empMob")==null?null:((Map)sellerLoginResult.get("data")).get("empMob").toString());
			seller.setSection_id(11);
			seller.setShop_id(user == null ? null :user.getId().intValue());
			seller.setStatus(1);
			seller.setCreater_id(user == null ? null :user.getId().intValue());
			sellerMapper.insertSelective(seller);
			System.out.println();
		}else{
			Seller sellerUpdate = new Seller();
			sellerUpdate.setId(sellerInfo.getId());
			sellerUpdate.setPhone(((Map)sellerLoginResult.get("data")).get("empMob")==null?null:((Map)sellerLoginResult.get("data")).get("empMob").toString());
			sellerUpdate.setZzh(((Map)sellerLoginResult.get("data")).get("orgNo")==null?null:((Map)sellerLoginResult.get("data")).get("orgNo").toString());
			sellerUpdate.setBmh(((Map)sellerLoginResult.get("data")).get("depNo")==null?null:((Map)sellerLoginResult.get("data")).get("depNo").toString());
			sellerUpdate.setShop_id(user == null ? null :user.getId().intValue());
			sellerMapper.updateByPrimaryKeySelective(sellerUpdate);
		}
		Seller sellerInfo1 = sellerMapper.selectOneBuUserName(seller.getUser_name());
		Integer loginPoint = sellerMapper.selectSellerLoginPoint(sellerInfo1.getId());
		if(loginPoint==0){
			SellerPointEntity spe=new SellerPointEntity();
			spe.setPoint(50);
			spe.setSeller_id(sellerInfo1.getId().longValue());
			spe.setTitle("登录积分");
			spe.setType_index(4);
			spe.setType_name("登录");
			sellerPointEntityMapper.insertSelective(spe);
		}
		
		String filiale = sellerMapper.selectFiliale(sellerInfo1.getShop_id());
		Map<String,Object> map=new HashMap<String, Object>();
		map.put("filiale", filiale);
		map.put("shop_id", sellerInfo1.getShop_id());
		map.put("id", sellerInfo1.getId());
		map.put("name", "04");
		map.put("phone", ((Map)sellerLoginResult.get("data")).get("empMob")==null?null:((Map)sellerLoginResult.get("data")).get("empMob").toString());
		map.put("user_name", seller.getUser_name());
		map.put("name", ((Map)sellerLoginResult.get("data")).get("empName")==null?null:((Map)sellerLoginResult.get("data")).get("empName").toString());
		map.put("head_url", sellerInfo1.getHead_url());
		Integer node3 = sellerMapper.selectNode3(sellerInfo1.getShop_id());
		map.put("node3", node3);
		message.setCode("0");
		message.setMessage("登录成功！");
		message.setResult(map);
		return message;
	}
	@Override
	public Map<String, Object> selectStatistics(Integer sellerId) {
		Map<String, Object> statistics = sellerMapper.selectStatistics(sellerId);
		return statistics;
	}
	@Override
	public Map<String, Object> selectTodayCustomer(Integer i) {
		Map<String, Object> todayCustomer = sellerMapper.selectTodayCustomer(i);
		return todayCustomer;
	}
	@Override
	public ResponseMessage<String> updateSellerPhone(Map<String, Object> map) {
		ResponseMessage<String> message = new ResponseMessage<String>();
		String string;
		try {
			string = CacheUtil.Cache1.get("SMS" + map.get("phone").toString());
			if (map.get("vcode").toString().equals(string)) {
				Integer sellerPhone = sellerMapper.updateSellerPhone(map);
				if(sellerPhone==1){
					message.setCode(SysConstant.SUCCESS_CODE);
					message.setMessage(SysConstant.SUCCESS_MSG);
					return message;
				}else{
					message.setCode(SysConstant.FAILURE_CODE);
					message.setMessage(SysConstant.FAILURE_MSG);
					return message;
				}
			}else{
				message.setCode("2");
				message.setMessage("短信验证码错误！");
				return message;
			}
		} catch (ExecutionException e) {
			e.printStackTrace();
			message.setCode(SysConstant.FAILURE_CODE);
			message.setMessage(SysConstant.FAILURE_MSG);
			return message;
		}
	}
	@Override
	public Map<String, Object> selectActivityInfo(Map<String, Object> map) {
		Map<String, Object> activityInfo = sellerMapper.selectActivityInfo(map);
		return activityInfo;
	}
	
	/**
	 * 导购登录接口的请求
	 * @throws Exception 
	 */
	public Map getSellerLoginResult(String orgNo,String depNo,String empMob,String sellerPwd){
		try {
			URI uri = new URI("http://mos.powerdekor.com.cn:8011/qobinf.ashx?type=GOI04&sn=qobi01.powerdekor.com.cn&orgNo="+ orgNo +"&depNo=" + depNo +"&empMob=" + empMob +"&empPin=" + sellerPwd);
			String sellerLoginJson = HttpClientUtil.sendGetForJson(uri);
			System.out.println(sellerLoginJson);
			Map map = (Map)JSON.parse(sellerLoginJson);
			return map;
		} catch (Exception e) {
			e.printStackTrace();
			return new HashMap();
		}
	}
	public Map getSellerZZResult(String orgNo) {
		try {
			URI uri = new URI("http://mos.powerdekor.com.cn:8011/qobinf.ashx?type=GOI02&sn=qobi01.powerdekor.com.cn&orgNo="+ orgNo);
			String sellerLoginJson = HttpClientUtil.sendGetForJson(uri);
			System.out.println(sellerLoginJson);
			Map map = (Map)JSON.parse(sellerLoginJson);
			return map;
		} catch (Exception e) {
			e.printStackTrace();
			return new HashMap();
		}
	}
	public Map getSellerZZBMResult(String orgNo,String depNo) {
		try {
			URI uri = new URI("http://mos.powerdekor.com.cn:8011/qobinf.ashx?type=GOI03&sn=qobi01.powerdekor.com.cn&orgNo="+ orgNo +"&depNo=" + depNo);
			String sellerLoginJson = HttpClientUtil.sendGetForJson(uri);
			System.out.println(sellerLoginJson);
			Map map = (Map)JSON.parse(sellerLoginJson);
			return map;
		} catch (Exception e) {
			e.printStackTrace();
			return new HashMap();
		}
	}

	public static void main(String[] args) {
		try {
//			getSellerLoginResult("028041", "SCDD01", "13882011576", "e10adc3949ba59abbe56e057f20f883e");
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	@Override
	public List<Map<String, Object>> selectSellerPoint(String phone) {
		List<Map<String,Object>> point = sellerMapper.selectSellerPoint(phone);
//		if(point!=null && point.size()>0){
//			String time=point.get(0).get("time").toString();
////			String token=phone+time;
////			String md5 = MD5Util.MD5(token);
////			RedisUtils.setValue("SELLER_POINT_TOKEN"+phone, phone);
////			String value = RedisUtils.getValue("SELLER_POINT_TOKEN"+phone);
////			System.out.println(value);
//		}
		return point;
	}
	@Override
	public List<Map<String, Object>> selectSellerInfo(String phone) {
		List<Map<String,Object>> point = sellerMapper.selectSellerInfo(phone);
		return point;
	}
	@Override
	public ResponseMessage<String> updateSellerPointStatus(Map<String, Object> map) {
		ResponseMessage<String> message = new ResponseMessage<String>();
		String token=map.get("token")+"";
//		String value = RedisUtils.getValue("SELLER_POINT_TOKEN"+map.get("phone"));
		String md5 = MD5Util.MD5(map.get("phone")+""+map.get("time"));
		if(md5.equals(token)){
			sellerMapper.updateSellerPointStatus(map);
			message.setCode(SysConstant.SUCCESS_CODE);
			message.setMessage(SysConstant.SUCCESS_MSG);
		}else{
			message.setCode("2");
			message.setMessage("token校验失败");
		}
		return message;
	}
	
	@Override
	public String selectNode3Image(Map<String, Object> map) {
		return sellerMapper.selectNode3Image(map);
	}
	@Override
	public List<Map<String,Object>> selectAccountInformation(Long user_id) {
		List<Map<String,Object>> list = null;
		if(user_id == 1064){
			list = sellerMapper.selectAccountInformation3(user_id);
		}else{
			list = sellerMapper.selectAccountInformation1(user_id);
		}
		return list;
	}
	@Override
	public Map<String, Object> selectSellerInfoById(Integer sellerId) {
		Map<String, Object> map = sellerMapper.selectSellerInfoById(sellerId);
		return map;
	}
	@Override
	public Page<Map<String, Object>> selectBiotopeInfoPage(Page<Map<String, Object>> page) {
		
		page.setData(sellerMapper.selectBiotopeInfoPage(page));
		return page;
	}
	@Override
	public Integer updateBiotopeInfo(BiotopeInfo biotopeInfo) {
		return sellerMapper.updateBiotopeInfo(biotopeInfo);
	}
	@Override
	public Map<String, Object> selectCustomerInfoByPhone(String phone) {
		Map<String, Object> map = sellerMapper.selectCustomerInfoByPhone(phone);
		if(map !=null){
			List<Map<String, Object>> list = sellerMapper.selectCustomerOrderByCustomerId(Long.parseLong(map.get("customerId").toString()));
			map.put("orderList", list);
		}
		
		return map;
	}
	@Override
	public List<Map<String, Object>> selectCustomerInfoListBySellerPhone(String phone) {
		
		return sellerMapper.selectCustomerInfoListBySellerPhone(phone);
	}
}
